package com.example.blog.service;

import com.example.blog.constants.Constant;
import com.example.blog.mapper.BlogMapper;
import com.example.blog.model.Blog;
import com.example.blog.model.UserInfo;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogService {
    @Autowired
    private BlogMapper blogMapper;

    public List<Blog> selectAll() {
        List<Blog> blogList = blogMapper.selectAll();
        for(Blog blog : blogList){
            StringBuilder stringBuilder = new StringBuilder();
            String content = blog.getContent();
            // 显示摘要
            if(content.length() > 150){
                char[] arr = content.toCharArray();
                for(int i = 0; i < 150; i++){
                    if(arr[i] != '#'){
                        stringBuilder.append(arr[i]);
                    }
                }
                blog.setContent(stringBuilder.toString().substring(0, 100) + "......");
            }
        }
        return blogList;
    }

    public Blog selectBlogById(Integer id) {
        return blogMapper.selectBlogById(id);
    }

    public Integer updateBlogById(Blog blog) {
        return blogMapper.updateBlogById(blog);
    }

    public Integer insertBlog(Blog blog) {
        return blogMapper.insertBlog(blog);
    }

    public Integer deleteBlogById(Integer id) {
        return blogMapper.deleteBlogById(id);
    }

    public Integer count(Integer userId) {
        return blogMapper.count(userId);
    }
}
